<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\Hash;    //引入hash

class SociatyAdminRepository
{

    /**
     * 根据ID或者管理员姓名获取管理员记录详情
     * @param $id 要查询的参数值
     * @param string $key 要查询的字段key值
     * @param bool|false $role_detail   是否查询管理员角色详情
     * @param bool|false $strict   是否按照严格模式查询,是则只查询正常启用的超管
     * @return mixed
     * @throws BasicException
     */
    public function admin_detail($id,$key='admin_id',$role_detail=false,$strict=false)
    {
        if(!in_array($key,['admin_id','admin_name'])) throw new BasicException(90001);

        //查询详情
        $admin = app('model_sc_admin')->where("$key",$id);

        if($role_detail)
        {
            if($strict) {
                $admin = $admin->with(['admin_role' => function ($query) {
                    $query->where('status',1);
                }]);
            }  else {
                $admin = $admin->with('admin_role');
            }
        }

        $admin = $admin->first();

        return $admin;
    }


    /**
     * 添加管理员
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function adminAdd($param)
    {
        $admin = app('model_sc_admin')->create($param);
        if($admin) return $admin;
        throw new BasicException(90006);
    }


    /**
     * 获取管理员列表
     * @param $sc_id
     * @param $param
     * @param int $page
     * @param int $perPage
     * @return mixed
     */
    public function adminList($param,$page=1,$perPage=10)
    {
        $list = app('model_sc_admin');
        if(!empty($param['status'])) $list = $list->where('disabled',$param['status']);
        if(!empty($param['role_id'])) $list = $list->where('role_id',$param['role_id']);
        if(!empty($param['search'])) $list = $list->where('admin_name','like',"%{$param['search']}%");

        $list = $list->select('admin_id','admin_name','email','avatar','role_id','addtime','last_login_time','last_login_ip','login_count')
            ->with('admin_role')->orderBy('addtime','desc')->paginate($perPage, ['*'], '', $page);

        foreach($list as &$val)
        {
            $val->addtime = date('Y-m-d H:i:s',$val->addtime);
            $val->last_login_time = date('Y-m-d H:i:s',$val->last_login_time);
            $val->role = isset($val->admin_role->role_name) ? $val->admin_role->role_name : '';
            unset($val->role_id);
            unset($val->admin_role);
        }

        return $list;
    }


    /**
     * 根据sc_id获取第一条注册的用户信息
     * @param $sc_id
     * @return mixed
     */
    public function getAdminDetailBy($sc_id)
    {
        $admin = app('model_sc_admin')->where('sc_id',$sc_id)->orderBy('add_time','desc')->first();

        return $admin;
    }


    /**
     * 获取厅的管理员详情
     * @param $sc_id
     * @return mixed
     */
    public function getHallAdminDetail($sc_id)
    {
        $admin = app('model_sc_admin')->where('sc_id',$sc_id)->with('admin_role')->first();
        return $admin;
    }

    /**
     * 获取所有管理员信息
     * @return mixed
     */
    public function getAllAdmins(){
      $admin = app('model_sc_admin')->select('admin_id')->get()->toArray();
      return $admin;
    }

    /**
     * 修改管理员密码
     * @param $admin_id
     * @param $password
     * @return mixed
     */
    public function savePassword($admin_id,$password){
        $admin =   app('model_sc_admin')->where('admin_id',$admin_id)->update(['new_password'=>$password]);
        return $admin;
    }







}


?>